package com.onpositive.wiki2.whole.model;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import com.onpositive.wiki2.miner.AbstractWikiPediaVisitor;
import com.onpositive.wiki2.miner.Page;

public class PageIndexer extends AbstractWikiPediaVisitor {

	protected ByteStringTable ts = new ByteStringTable(5000 * 1000);

	File ps;

	public PageIndexer(String string) {
		ps = new File(string);
	}

	@Override
	protected void onPage(Page page) {
		if (page.id > Integer.MAX_VALUE) {
			throw new RuntimeException();
		}
		int id = ts.id(page.title);
		if (id != -1) {
			throw new RuntimeException();
		}
		ts.allocateId(page.title);
	}

	public void close() throws FileNotFoundException, IOException {
		ts.trim();
		File file = new File(ps, "content.cat");
		file.getParentFile().mkdirs();
		if(file.exists())
			file.delete();
		
		file.createNewFile();
		
		ObjectOutputStream z = new ObjectOutputStream(new BufferedOutputStream(
				new FileOutputStream(file)));
		z.writeObject(ts);
		z.close();
	}
}
